<!--
 * @Description: 全单退菜功能
 * @Author: duan_shuangjun 丶
 * @Date: 2025-01-16 14:25:30
-->
<template>
  <custom-dialog v-model:visible="dialogVisible" curent-class="full-return-food-dialog">
    <div class="dialog-content">
      <div class="title">全单退菜</div>
      <div class="description">
        <div class="count">共{{ totalFoodCount }}份菜品</div>
      </div>
      <div class="scroll-content">
        <div class="position-bgc"></div>
        <div class="title">退菜原因</div>
        <div class="scroll-list" v-loading="reasonLoading">
          <div
            class="scroll-item"
            :class="{ 'scroll-item-active': selectActiveIndex === index }"
            v-for="(item, index) in reasonList"
            :key="item.name + index"
            @click="handleSelectReason(index, item.name)"
          >
            {{ item.name }}
          </div>
        </div>
        <!-- <div v-else style="padding: 0 40px">
          <el-skeleton :rows="3" animated />
        </div> -->
      </div>
      <div class="form-box">
        <div class="form-item">
          <div class="desc">退菜原因</div>
          <div class="dialog-input">
            <input
              class="input"
              v-model.trim="reasonKeyword"
              type="text"
              placeholder="请在上方选择或输入退菜原因"
            />
            <img
              class="image"
              src="@/assets/images/home/input-clear.png"
              alt=""
              @click="handleClearInput"
              v-if="reasonKeyword"
            />
          </div>
        </div>
      </div>
      <div class="btn-box">
        <el-button class="cancel-btn" @click="dialogVisible = false">取消</el-button>
        <el-button class="confirm-btn" @click="handleConfirm" :loading="loading">
          {{ loading ? '处理中...' : '确定退菜' }}
        </el-button>
      </div>
    </div>
  </custom-dialog>
</template>

<script setup lang="ts">
import { ref, computed, watch, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { orderFoodBatchBack } from '@/api/desktop'
import { foodReasonList } from '@/api/food'
import CustomDialog from '@/components/CustomDialog/Index.vue'
import { ElButton } from 'element-plus'

interface Props {
  deskDetail: any
  modelValue: boolean
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'refreshData'): void
  (e: 'refreshDesk'): void
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: false,
  deskDetail: () => ({})
})

const emit = defineEmits<Emits>()

// 对话框显示状态
const dialogVisible = computed({
  get: () => props.modelValue,
  set: val => emit('update:modelValue', val)
})

// 加载状态
const loading = ref(false)
// 退菜原因关键词
const reasonKeyword = ref('')
// 退菜原因列表
const reasonList = ref<{ name: string }[]>([])
// 退菜原因加载中
const reasonLoading = ref(false)
// 选中对应原因的索引
const selectActiveIndex = ref(-1)

// 计算总菜品数量
const totalFoodCount = computed(() => {
  if (!props.deskDetail?.foodList) return 0
  return props.deskDetail.foodList.reduce((total: number, food: any) => {
    return total + (food.num || 0)
  }, 0)
})

// 获取菜品退菜的原因列表
const getFoodReasonList = async () => {
  try {
    reasonLoading.value = true
    const res = await foodReasonList({ type: 10 })
    if (res.status === 200) {
      const dataList: { name: string }[] = []
      res.data.forEach((item: string) => {
        dataList.push({ name: item })
      })
      reasonList.value = dataList
    }
  } catch (error) {
    console.error('获取退菜原因列表失败:', error)
  } finally {
    reasonLoading.value = false
  }
}

// 清空退菜原因输入框
const handleClearInput = () => {
  reasonKeyword.value = ''
  selectActiveIndex.value = -1
}

// 选中对应的原因
const handleSelectReason = (index: number, name: string) => {
  selectActiveIndex.value = index
  reasonKeyword.value = name
}

// 重置数据
const resetData = () => {
  selectActiveIndex.value = -1
  reasonKeyword.value = ''
}

// 监听对话框打开状态
watch(
  () => dialogVisible.value,
  val => {
    if (val) {
      // 获取菜品退菜的原因列表
      getFoodReasonList()
      // 重置数据
      resetData()
    }
  }
)

// 确认全单退菜处理
const handleConfirm = async () => {
  if (!reasonKeyword.value) {
    ElMessage.warning('退菜原因不能为空')
    return
  }

  if (loading.value) return

  try {
    loading.value = true

    const params = {
      record_id: props.deskDetail.id,
      is_all: 1, // 1: 全单退菜
      reason: reasonKeyword.value, // 退菜原因
      print: 1 // 打印退菜单
    }

    const res = await orderFoodBatchBack(params)
    if (res.status === 200) {
      ElMessage.success(res.data?.message || '全单退菜成功')
      dialogVisible.value = false
      emit('refreshData')
      emit('refreshDesk')
    } else {
      ElMessage.error(res.data?.message || '全单退菜失败')
    }
  } catch (error) {
    console.error('全单退菜失败:', error)
    ElMessage.error('全单退菜失败')
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.dialog-content {
  .title {
    font-size: 20px;
    font-family:
      PingFang SC,
      PingFang SC-Bold;
    font-weight: 700;
    text-align: center;
    color: #353535;
  }

  .description {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 16px;
    font-size: 16px;
    font-family:
      PingFang SC,
      PingFang SC-Regular;
    font-weight: 400;
    text-align: center;
    color: #353535;

    .count {
      margin-left: 16px;
    }
  }

  .position-bgc {
    position: absolute;
    left: -50px;
    top: 120px;
    z-index: 1;
    background: #f4f4f4;
  }

  .scroll-content {
    position: relative;
    height: 190px;
    box-sizing: border-box;
    background: #f4f4f4;
    padding: 20px 0;
    overflow-y: auto;
    margin-top: 20px;

    .title {
      position: relative;
      z-index: 2;
      font-size: 16px;
      font-family:
        PingFang SC,
        PingFang SC-Regular;
      font-weight: 400;
      text-align: left;
      color: #333333;
      padding: 0px 40px;
    }

    .scroll-list {
      width: 516px;
      position: relative;
      z-index: 2;
      padding: 0px 40px;
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      align-items: center;
      column-gap: 20px;
      // justify-content: space-between;
      flex-wrap: wrap;

      .scroll-item {
        min-width: 120px;
        height: 48px;
        line-height: 48px;
        background: #ffffff;
        border: 1px solid #d2d2d2;
        border-radius: 12px;
        font-size: 14px;
        font-family:
          PingFang SC,
          PingFang SC-Regular;
        font-weight: 400;
        text-align: center;
        color: #333333;
        margin-top: 20px;
        cursor: pointer;
      }

      .scroll-item-active {
        border-color: #7c0a03 !important;
        color: #7c0a03 !important;
      }
    }
  }

  // flex布局的列表进行占位操作
  .scroll-list-item-zhanwei {
    min-width: 120px;
    overflow: hidden;
  }

  //滚动条整体的大小
  .scroll-content::-webkit-scrollbar {
    width: 6px;
    height: 0;
  }

  //滚动条里滑动的滑块
  .scroll-content::-webkit-scrollbar-thumb {
    height: 90px;
    background: #d2d2d2;
    border-radius: 3px;
  }

  //滚动条底层样式
  .scroll-content::-webkit-scrollbar-track {
    display: none;
  }

  .form-box {
    padding: 30px 40px;

    .form-item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-top: 20px;

      .desc {
        font-size: 16px;
        font-family:
          PingFang SC,
          PingFang SC-Regular;
        font-weight: 400;
        color: #333333;
      }

      .dialog-input {
        width: 420px;
        height: 48px;
        background: #ffffff;
        border: 1px solid #d2d2d2;
        border-radius: 12px;
        box-sizing: border-box;
        padding: 0 20px;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .input {
          width: 400px;
          font-size: 14px;
          font-family:
            PingFang SC,
            PingFang SC-Regular;
          font-weight: 400;
          text-align: left;
          color: #333333;
          outline: none;
          border: none;
        }

        .image {
          flex-shrink: 0;
          width: 24px;
          height: 24px;
          cursor: pointer;
        }
      }
    }
  }

  .btn-box {
    padding: 50px 38px 0 38px;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .cancel-btn {
      width: 190px;
      height: 52px;
      border: 1px solid #d2d2d2;
      border-radius: 12px;
      font-size: 18px;
      font-family:
        PingFang SC,
        PingFang SC-Regular;
      font-weight: 400;
      text-align: center;
      color: #353535;
      line-height: 52px;
      cursor: pointer;
    }

    .confirm-btn {
      width: 190px;
      height: 52px;
      background: #7c0a03;
      border-radius: 12px;
      font-size: 18px;
      font-family:
        PingFang SC,
        PingFang SC-Regular;
      font-weight: 400;
      text-align: center;
      color: #ffffff;
      line-height: 52px;
      cursor: pointer;

      &.loading {
        opacity: 0.7;
        cursor: not-allowed;
      }
    }
  }
}
</style>
<style>
.full-return-food-dialog {
  padding: 50px 0px !important;
}
</style>
